package com.senven.common.pojo.aop;

import com.alibaba.nacos.api.utils.StringUtils;
import com.senven.common.entity.Logincn;
import com.senven.common.entity.User;
import com.senven.common.pojo.controller.LoginController;
import com.senven.common.pojo.feign.UserFeign;
import com.senven.common.pojo.vo.CommonResult;
import com.senven.common.pojo.vo.LoginVo;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.bouncycastle.asn1.ocsp.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;

@Component
@Aspect
public class LoginAop {
    @Autowired
    private UserFeign userFeign;

    @Pointcut("execution(* com.senven.common.pojo.controller.LoginController.login(..))")
    public void loginSucessPointCut(){};

    @AfterReturning(pointcut = "loginSucessPointCut()",returning = "result")
    public void loginSuccess(JoinPoint joinPoint, CommonResult result){
        System.out.println("登录成功");
        Object[] args = joinPoint.getArgs();
        System.out.println("登录者的账号:"+args[0]);
        LoginVo arg = (LoginVo) args[0];
        System.out.println(arg);
        String username = arg.getUsername();
        Object data = result.getData();
        Integer code = result.getCode();
        String msg = result.getMsg();


        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
//
//        String remoteAddr = request.getRemoteAddr();
//        System.out.println("登录者的ip:"+remoteAddr);
        String remoteAddr = getIpAddr(request);


//
        System.out.println("登录的状态:"+result.getCode());
        System.out.println("登录的提示信息:"+result.getMsg());
//
        Logincn loginCN = new Logincn(null,username,remoteAddr, result.getCode(), result.getMsg(), LocalDateTime.now());
        userFeign.insertLogincn(loginCN);
    }

    /**
     * 获取IP地址
     *
     */
    public static String getIpAddr(HttpServletRequest request)
    {
        String ip = request.getHeader("X-Real-IP");
        if(!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip))
            return ip;
        ip = request.getHeader("X-Forwarded-For");
        if(!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip))
        {
            int index = ip.indexOf(',');
            if(index != -1)
                return ip.substring(0, index);
            else
                return ip;
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
            ip = request.getHeader("Proxy-Client-IP");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
            ip = request.getHeader("WL-Proxy-Client-IP");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
            ip = request.getHeader("HTTP_CLIENT_IP");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
            ip = request.getRemoteAddr();
        return ip;
    }

//
//    @AfterReturning(value = "execution(* com.senven.common.pojo.controller.LoginController.login(..))",returning = "r")
//    public void LoginAfterReturn(JoinPoint joinPoint,Object r){
//        Object[] args = joinPoint.getArgs();
//        System.out.println("登录者的账号:"+args[0]);
//        User arg = (User) args[0];
//        String id = arg.getId();
//        System.out.println(id);
//
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
//
//        String remoteAddr = request.getRemoteAddr();
//        System.out.println("登录者的ip:"+remoteAddr);
//
//        CommonResult result = (CommonResult) r;
//
//        System.out.println("登录的状态:"+result.getCode());
//        System.out.println("登录的提示信息:"+result.getMsg());
//
//        Logincn loginCN = new Logincn(null,id,remoteAddr, result.getCode(), result.getMsg(),new Date());
//        userFeign.insertLogincn(loginCN);
//    }
}
